module.exports = (function() {
    var express = require('express');
    var router = express.Router();
    var path = require('path');
    var uid = require('uid');
    var request = require('request').defaults({
        json: true
    });
    var config = require('./../config.json');

    router.get('/', function(req, res) {
        if (!req.cookies.password) {
            res.redirect('/register1');
        } else {
            res.sendFile(path.join(__dirname, './authenticateViews/register2.html'));
        }
    });

    router.get('/corporation', function(req, res) {
        request({
            method: "get",
            url: config.adminBaseUrl + "/corporation"
        }, function(err, response, body) {
            if (!err && response.statusCode == 200) {
                if (typeof(body) === 'string') {
                    body = JSON.parse(body);
                }
                res.send(body);
            } else {
                res.send([]);
            }
        });
    });

    var redis = require('redis');
    router.post('/', function(req, res) {
        for (var c in req.cookies) {
            if (c === 'username') {
                continue;
            } else {
                res.clearCookie(c);
            }
        }
        var applierInfo = req.body;
        request({
            url: config.authorUrl + "/user/applyaccount",
            json: applierInfo,
            method: 'post'
        }, function(err, response, body) {
            if (!err && response.statusCode == 200) {
                res.redirect('/login');
            } else {
                res.send('发生错误!');
            }
        });
        // 
    });
    return router;
})();
